package com.ctrlspace.core.util;

public class JavaUtil {
	
	public static String toNameSafeString(String string) {
		StringBuilder output = new StringBuilder();
		
		// Convert!
		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			
			// First is invalid indentifier
			if (i == 0 && Character.isJavaIdentifierStart(c) == false) {
				output.append('_');
				continue;
			}
			
			// Is Space
			if (Character.isWhitespace(c) == true) {
				output.append('_');
				continue;
			}
			
			// Others are ignorable
			if (Character.isJavaIdentifierPart(c) == true) {
				output.append(c);
				continue;
			}
		}

		return output.toString();
	}
}